/**
 * 
 */
package hu.rsc.forbiddenwords.client.table;

import java.util.List;

import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
import com.google.gwt.gen2.table.client.ColumnDefinition;
import com.google.gwt.gen2.table.client.PagingScrollTable;
import com.google.gwt.gen2.table.client.TableDefinition;
import com.google.gwt.gen2.table.client.TableModel;
import com.google.gwt.gen2.table.client.TableModelHelper.ColumnSortInfo;
import com.google.gwt.gen2.table.event.client.ColumnSortEvent;
import com.google.gwt.gen2.table.event.client.ColumnSortHandler;
import com.google.gwt.user.client.Element;

/**
 * @author vargaa
 * 
 */
public class AdvancedPagingScrollTable<RowType> extends PagingScrollTable<RowType> {

    private String tableTitle;

    public AdvancedPagingScrollTable(TableModel<RowType> tableModel, TableDefinition<RowType> tableDef) {
	this(tableModel, tableDef, "Iktatás", 0);
    }

    public AdvancedPagingScrollTable(TableModel<RowType> tableModel, TableDefinition<RowType> tableDef, String tableTitle) {
	this(tableModel, tableDef, tableTitle, 0);
    }

    public AdvancedPagingScrollTable(TableModel<RowType> tableModel, TableDefinition<RowType> tableDef, String tableTitle, int defaultSortColumnIndex) {
	this(tableModel, tableDef, tableTitle, defaultSortColumnIndex, true);
    }

    public AdvancedPagingScrollTable(TableModel<RowType> tableModel, TableDefinition<RowType> tableDef, String tableTitle, int defaultSortColumnIndex,
	    boolean ascending) {
	super(tableModel, tableDef);
	this.tableTitle = tableTitle;

	for (Object element2 : this.getTableDefinition().getVisibleColumnDefinitions()) {
	    AbstractColumnDefinition colDef = (AbstractColumnDefinition) element2;
	    colDef.setHeader(1, this.tableTitle);
	}
	this.getDataTable().addColumnSortHandler(new ColumnSortHandler() {

	    @Override
	    public void onColumnSorted(ColumnSortEvent event) {
		// minden headerbol ikon torlese
		for (ColumnDefinition columnDef : AdvancedPagingScrollTable.this.getTableDefinition().getVisibleColumnDefinitions()) {
		    if (columnDef instanceof AdvancedAbstractColumnDefinition) {
			((AdvancedAbstractColumnDefinition) columnDef).clearSortIndicator();
		    }
		}
		ColumnDefinition cd = AdvancedPagingScrollTable.this.getTableDefinition().getVisibleColumnDefinitions()
			.get(event.getColumnSortList().getPrimaryColumn());
		if (cd instanceof AdvancedAbstractColumnDefinition) {
		    ((AdvancedAbstractColumnDefinition) cd).applySortingIndicator(event.getColumnSortList().isPrimaryAscending());
		}
	    }
	});
	this.getDataTable().getColumnSortList().add(new ColumnSortInfo(defaultSortColumnIndex, ascending));
	if (this.getColumnDefinition(defaultSortColumnIndex) instanceof AdvancedAbstractColumnDefinition) {
	    ((AdvancedAbstractColumnDefinition) this.getColumnDefinition(defaultSortColumnIndex)).applySortingIndicator(ascending);
	}
    }

    public List<RowType> getRows() {
	return super.getRowValues();
    }

    @Override
    protected void applySortedColumnIndicator(Element tdElem, boolean ascending) {
	super.applySortedColumnIndicator(null, ascending);
    }
}
